package com.scau.action.business;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ModelDriven;
import com.scau.action.BaseAction;
import com.scau.exception.BusinessException;
import com.scau.model.business.Purchase;
import com.scau.service.impl.business.PurchaseService;
import com.scau.util.PageController;

@Component
@Scope("prototype")
public class PurchaseAction extends BaseAction implements Serializable {
	private static final long serialVersionUID = 8299975587235537983L;
	private final static Log logger = LogFactory.getLog(OrderListAction.class);
	private PageController pager = null;
	private PurchaseService purchaseService = null;
	private Purchase purchase = null;

	
	/**
	 * 列表显示
	 * @return
	 */
	public String list() {
		int totalRows = purchaseService.getRecordCount(new Purchase());
		String URL = getListURL();
		this.pager.setURL(URL);
		this.pager.setTotalRowsAmount(totalRows);
		List<Purchase> resourceList = purchaseService.list(
				new Purchase(), this.pager.getPageStartRow(), pager
						.getPageSize(), null,null);
		pager.setData(resourceList);
		return "list";
	}

	/**
	 * 修改
	 * @return
	 */
	public String get() {
		purchase = purchaseService.get(purchase);
		return "edit";
	}
	
	/**
	 * 保存
	 * @return
	 */
	public String save() {
		try {
			purchaseService.save(purchase);
			return list();
		} catch (BusinessException e) {
			// 保存原来表单已输入的内容
			request.setAttribute("purchase", purchase);
			request.setAttribute("message", e.getMessage());
			return "edit";
		}
	}
	
	/**
	 * 删除
	 * @return
	 */
	public String del() {
		String[] ids = request.getParameterValues("id");
		for (String id : ids) {
			purchase=new Purchase();
			if (null != id && !("".equals(id))) {
				purchaseService.delete(purchase,Long.parseLong(id));
			}
		}
		return list();
	}
	

	public PageController getPager() {
		return pager;
	}

	@Resource
	public void setPager(PageController pager) {
		this.pager = pager;
	}

	public PurchaseService getPurchaseService() {
		return purchaseService;
	}

	@Resource
	public void setPurchaseService(PurchaseService purchaseService) {
		this.purchaseService = purchaseService;
	}

	public Purchase getPurchase() {
		return purchase;
	}

	public void setPurchase(Purchase purchase) {
		this.purchase = purchase;
	}


}
